package org.example.chapter4.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.example.chapter4.aspect.validator.UserValidator;
import org.example.chapter4.aspect.validator.impl.UserValidatorImpl;
import org.example.chapter4.pojo.User;

/**
 * @author CoderXshuai
 * @date 2020/7/26/0026 18:08
 */

@Aspect
public class MyAspect {
    @DeclareParents(value = "org.example.chapter4.aspect.service.impl.UserServiceImpl+"
            , defaultImpl = UserValidatorImpl.class)
    public UserValidator userValidator;

    @Pointcut("execution(* org.example.chapter4.aspect.service.impl.UserServiceImpl.printUser(..))")
    public void pointCut() {
    }

    @Around("pointCut()")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("around before......");
        jp.proceed();
        System.out.println("around after......");
    }

    @Before("pointCut()")
    public void before() {
        System.out.println("before ......");
    }

    @Before("pointCut()&&args(user)")
    public void beforeParam(JoinPoint point, User user) {
        Object[] args = point.getArgs();
        System.out.println("before ......");
    }

    @After("pointCut()")
    public void after() {
        System.out.println("after ......");
    }

    @AfterReturning("pointCut()")
    public void afterReturning() {
        System.out.println("afterReturning ......");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing() {
        System.out.println("afterThrowing ......");
    }
}
